package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.admin.AdminCommonDaoImpl;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.admin.AdminResult2;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.web.controller.usercenter.newadmin.CenterReloadHelper;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/usercenter/admin")
public class CenterForbiddenAdminController {

    /**
     *  20.1 白名单增删改查（微信排行）
     */
    @ResponseBody
    @RequestMapping(value = "wechatRankForbidden", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String wechatRankForbidden(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        int type = jsonObject.getIntValue("type");
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(type != 0){
            JSONArray jsonArray = jsonObject.getJSONArray("openIds");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<>();
                map.put("openId", object.getString("openId"));
                map.put("remark", object.getString("remark"));
                dataList.add(map);
            }
        }
        AdminResult2 result2 = AdminResult2.defaultInstance();
        if(type == 0){
            List<HashMap<String, Object>> list = new AdminCommonDaoImpl().getWechatBlackUserList(channelId);
            for (HashMap<String, Object> map : list) {
                result2.getData().add(map);
            }
        }else if(type == 1){
            //新增
            boolean result = new AdminCommonDaoImpl().batchInsertWechatRankBlankUser(channelId, dataList);
            if(!result){
                AdminResult2.failMsg("数据库操作失败");
            }
        }else if(type == 2){
            //删除
            boolean result = new AdminCommonDaoImpl().batchDeleteWechatRankBlankUser(channelId, dataList);
            if(!result){
                AdminResult2.failMsg("数据库操作失败");
            }
        }else if(type == 3){
            //修改
            boolean result = new AdminCommonDaoImpl().batchUpdateWechatRankBlankUser(channelId, dataList);
            if(!result){
                AdminResult2.failMsg("数据库操作失败");
            }
        }else {
            return AdminResult.failMsg("无效参数").toJsonString();
        }

        return result2.toJsonString();
    }


    /**
     * 20.2 白名单刷新（微信排行）
     */

    @ResponseBody
    @RequestMapping(value = "wechatRankForbiddenReload", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String wechatRankForbiddenReload(HttpServletRequest request, @RequestBody JSONObject jsonObject){

        int channelId = jsonObject.getIntValue("channelId");
        //刷新区服
        List<ServerInfo> channelServerList = ServerListMgr.getChannelServerList(channelId);

        AdminResult adminResult = AdminResult.defaultInstance();

        String msg = CenterReloadHelper.serverMgrReload(channelServerList, "WechatRankBlackUserMgr", 30);
        adminResult.setMsg(msg);
        return adminResult.toJsonString();
    }
}
